package com.eshopkeeper.web.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.eshopkeeper.core.base.BaseSysController;
import com.eshopkeeper.core.util.Request2ModelUtil;
import com.eshopkeeper.core.util.WebUtil;
import com.eshopkeeper.model.sys.SysRole;
import com.eshopkeeper.model.sys.ext.SysMenuBean;
import com.eshopkeeper.service.sys.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenshufeng
 * @Description: 角色管理
 * @date 2017年3月7日
 */
@RestController
@Api(value = "角色管理", description = "角色管理")
@RequestMapping(value = "role", method = RequestMethod.POST)
public class SysRoleController extends BaseSysController {
    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "查询角色")
    //@RequiresPermissions("sys.role.read")
    @RequestMapping(value = "/read/list")
    public Object get(HttpServletRequest request, ModelMap modelMap) {
        Map<String, Object> params = WebUtil.getParameterMap(request);
        Page<?> list = sysRoleService.queryBean(params);
        return setSuccessModelMap(modelMap, list);
    }

    // 详细信息
    @ApiOperation(value = "角色详情")
    //@RequiresPermissions("sys.role.detail")
    @RequestMapping(value = "/read/detail")
    public Object detail(ModelMap modelMap, @RequestParam(value = "id", required = false) Long id) {
        SysRole record = sysRoleService.queryById(id);
        return setSuccessModelMap(modelMap, record);
    }

    // 新增
    @ApiOperation(value = "添加角色")
    //@RequiresPermissions("sys.role.add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object add(HttpServletRequest request, ModelMap modelMap) {
        SysRole record = Request2ModelUtil.covert(SysRole.class, request);
        sysRoleService.add(record);
        return setSuccessModelMap(modelMap);
    }

    // 修改
    @ApiOperation(value = "修改角色")
    //@RequiresPermissions("sys.role.update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(HttpServletRequest request, ModelMap modelMap) {
        SysRole record = Request2ModelUtil.covert(SysRole.class, request);
        sysRoleService.update(record);
        return setSuccessModelMap(modelMap);
    }

    // 删除
    @ApiOperation(value = "删除角色")
    //@RequiresPermissions("sys.role.delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Object delete(HttpServletRequest request, ModelMap modelMap,
                         @RequestParam(value = "id", required = false) Long id) {
        sysRoleService.delete(id);
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation(value = "获取角色权限")
    //@RequiresPermissions("sys.role.getPermission")
    @RequestMapping(value = "/read/permission", method = RequestMethod.POST)
    public Object getPermission(HttpServletRequest request, ModelMap modelMap,
                                @RequestParam(value = "id", required = false) Long id) {
        List<String> permissions = sysRoleService.getPermissions(id);
        return setSuccessModelMap(modelMap, permissions);
    }
    
    // 启用禁用
 	@ApiOperation(value = "修改状态")
 	//@RequiresPermissions("sys.role.disable")
 	@RequestMapping(value = "/update/disable")
 	public Object disable(ModelMap modelMap, @RequestParam(value = "id", required = false) Long id,
 			@RequestParam(value = "enable", required = false) Integer enable) {
 		Map<String, Object> paramMap = new HashMap<String, Object>();
 		paramMap.put("id", id);
 		paramMap.put("enable", enable);
 		sysRoleService.updateEnable(paramMap);
 		return setSuccessModelMap(modelMap);
 	}
 	
 	@ApiOperation(value = "获取角色菜单列表")
    //@RequiresPermissions("sys.role.read")
    @RequestMapping(value = "/read/getMenuList", method = RequestMethod.POST)
    public Object getMenuList(HttpServletRequest request, ModelMap modelMap,
                                @RequestParam(value = "id", required = false) Long id) {
        List<SysMenuBean> permissions = sysRoleService.getMenuList(id);

        return setSuccessModelMap(modelMap, permissions);
    }
 	
  	@ApiOperation(value = "修改角色菜单权限")
  	//@RequiresPermissions("sys.role.updateSysRoleMenu")
  	@RequestMapping(value = "/update/updateSysRoleMenu")
  	public Object updateSysRoleMenu(ModelMap modelMap, @RequestParam(value = "roleId", required = false) String roleId,
  			@RequestParam(value = "menuIds", required = false) String menuIds) throws Exception {
  		Map<String, Object> paramMap = new HashMap<String, Object>();
  		if(menuIds!=null&&menuIds.indexOf(",")!=-1){
  	  		paramMap.put("menuIds", menuIds.substring(0, menuIds.lastIndexOf(",")));
  	  		paramMap.put("roleId", roleId);
  	  		sysRoleService.updateSysRoleMenu(paramMap);
  		}else{
  			sysRoleService.clearSysRoleMenu(Long.parseLong(roleId));
  		}
  		return setSuccessModelMap(modelMap);
  	}
  	
  	@ApiOperation(value = "获取角色菜单功能列表")
    //@RequiresPermissions("sys.role.read")
    @RequestMapping(value = "/read/getMenuFuncList", method = RequestMethod.POST)
    public Object getMenuFuncList(HttpServletRequest request, ModelMap modelMap,
                                @RequestParam(value = "id", required = false) Long id) {
        List<SysMenuBean> permissions = sysRoleService.getMenuFuncList(id);
        return setSuccessModelMap(modelMap, permissions);
    }
  	
  	@ApiOperation(value = "修改角色菜单功能权限")
  	//@RequiresPermissions("sys.role.update")
  	@RequestMapping(value = "/update/updateSysRoleMenuFunc")
  	public Object updateSysRoleMenuFunc(ModelMap modelMap, @RequestParam(value = "roleId", required = false) String roleId,
  			@RequestParam(value = "arryFunc", required = false) String menuIds) throws Exception {
  		Map<String, Object> paramMap = new HashMap<String, Object>();
  		if(menuIds!=null&&menuIds.indexOf(",")!=-1){
  	  		paramMap.put("arryFunc", menuIds.substring(0, menuIds.lastIndexOf(",")));
  	  		paramMap.put("roleId", roleId);
  	  		sysRoleService.updateSysRoleMenuFunc(paramMap);
  		}else{
  			sysRoleService.clearSysRoleMenuFunc(Long.parseLong(roleId));
  		}
  		return setSuccessModelMap(modelMap);
  	}
}
